<?php
/**
 * SocialEngine
 *
 * @category   Application_Widget
 * @package    Feed-Autoload
 * @copyright  Copyright 2012 GeoDeveloper.net
 * @author     GeoDeveloper.net
 */
$this->headScript()
    ->appendFile($this->layout()->staticBaseUrl . 'application/widgets/feed-autoload/externals/scripts/ScrollSpy.js');
?>
<script type="text/javascript">
function loadMore(){
	console.log("loadMore");
	$('feed_viewmore_link').fireEvent('click', [{stop: function(){}}, 'loadMore'], 500);
}

window.addEvent('domready', function() {
    var limit = <?php echo $this->limit ?>;
    var count = 0;
    var moreButton = $('feed_viewmore');
    var loading = $('feed_loading');  
    new SmoothScroll({duration:500});
    var totalHeight, currentScroll, visibleHeight, sheight;
    var ss = new ScrollSpy({
        min: 0,
        onTick: function(position,state,enters,leaves) {
            var moved = true;
            if (document.documentElement.scrollTop)
            {
                currentScroll = document.documentElement.scrollTop;
            }
            else
            {
                currentScroll = document.body.scrollTop;
            }
            totalHeight = document.body.offsetHeight;
            visibleHeight = document.documentElement.clientHeight;
            sheight = currentScroll + visibleHeight;
            
            if(count == 0 && totalHeight == sheight)
                moved = false;
            
            if (totalHeight - 100 <= sheight && moved == true && (count+1 <= limit || limit == 0))
            {
                if(moreButton.get('style') == ""){
                loadMore();
                count++;
                }
            }  
        }
    });
   
});
</script>